<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/21
 * Time: 上午9:25
 */
class PubFilter extends LibFilter {

    public function addFilter($name, $type, $title = '', $args = array(),$l_type = 0) {

        if ($args['enforce'] != true) {
            $value = PubRequest::R($name);
            //判断是否日志类且由菜单点击进来的查询
            $log_s = PubRequest::R('log_s');
            if(!isset($log_s) && $l_type == 1){
            	$params_s = PubAuthSession::get('params_s');
            	if($name == 'special_id'){
            		$value = $params_s['special_id'];
            	}
            	if($name == 'os'){
            		$value = $params_s['os'];
            	}
            	if($name == 'server'){
            		$value = $params_s['server'];
            	}
            	if($name == 'port'){
            		$value = $params_s['port'];
            	}
            	if($name == 'time'){
            		$value = $params_s['time'];
            	}
            }
            
            
            $ana_s = PubRequest::R('ana_s');
            if(!isset($ana_s) && $l_type == 2){
            	$params_s = PubAuthSession::get('ana_s');
            	if($name == 'special_id'){
            		$value = $params_s['special_id'];
            	}
            	if($name == 'os'){
            		$value = $params_s['os'];
            	}
            	if($name == 'server'){
            		$value = $params_s['server'];
            	}
            	if($name == 'time_start'){
            		$value = $params_s['time_start'];
            	}
            	if($name == 'time_end'){
            		$value = $params_s['time_end'];
            	}
            }            
            
        }

        if (!isset($value))
            $value = $args['value'];
        if (is_string($value))
            $value = trim($value);

        $this->filter[$name] = array(
            'name' => $name,
            'title' => $title,
            'type' => $type,
            'value' => $value,
            'args' => $args
        );

        return $this;
    }

    public function get() {
        $result = parent::get();
        
        if (!isset($result['g']) && isset($_REQUEST['g'])) {
        	$result['g'] = PubRequest::R('g');
        }

        if (!isset($result['p']) && isset($_REQUEST['p'])) {
            $result['p'] = PubRequest::R('p');
        }

        if (!isset($result['page']) && isset($_REQUEST['page'])) {
            $result['page'] = PubRequest::R('page');
        }

        if (!isset($result['length']) && isset($_REQUEST['length'])) {
            $result['length'] = PubRequest::R('length');
        }

        if (!isset($result['order']) && isset($_REQUEST['order'])) {
            $result['order'] = PubRequest::R('order');
        }

        if (isset($result['af_channel'])) {
            $options = AffiliateService::getUser();
            if (!isset($options[$result['af_channel']])) {
                echo '没有权限';
                die;
            }
        }

        return $result;
    }

    public function addChannel($game='mango'){
        $channel = PubDict::dict($game, 'channel');

        $channel = array('-1' => '--所有渠道--') + $channel;

        $this->addFilter('cid', 'select', '渠道', array(
            'options'   => $channel,
            'value'     => empty($channel) ? '' : key($channel)
        ));

        return $this;
    }

    public function addOssServer($all = false){
        $platform = PubBase::getCurrentPlatform();
        $game = PubBase::getCurrentGame();

        $serverList = PubServer::getOptions($platform, $game, 'id');
        
        if($all){
        	$serverList = array('0' => '所有') + $serverList;
        }
        
        $this->addFilter('server', 'searchableSelect', '服务器', array(
            'options'   => $serverList,
            'value'     => empty($serverList) ? '' : key($serverList)
        ));

        return $this;
    }
    
    public function addOssServerAll($all = false){    	
    	$serverList = array('' => '请选择');
    
    	$this->addFilter('server', 'searchableSelect', '服务器', array(
    			'options'   => $serverList,
    			'value'     => empty($serverList) ? '' : key($serverList)
    	));
    
    	return $this;
    }
    
    public function addSpecial($all = true){
    	$user = AccAuth::me();
    	$specialList = array();
//     	if($user['IsAdmin'] != 1){
//     		$options = PubAuthSession::get('os');		
//     		if($options){
//     			$tabs = '';
//     			foreach ($options as $key=>$val){
//     				$tabs .= ','.'"'.$key.'"';
//     			}
//     			$tabs = substr($tabs, 1);
    			 
//     			$modChannel = new ModChannel();
//     			$special = $modChannel->getSpecialForChannel($tabs);
//     			$specialList = array();
//     			foreach ($special as $key=>$val){
//     				$specialList[$val['special_id']] = $val['name'];
//     			}
//     		}
//     	}else{
//     		$specialList = SpecialServerService::specialList();
//     	}
    	
    	$specialList = SpecialServerService::specialList();
    	
//     	if($all && $user['IsAdmin'] == 1){
//     		$specialList = array('' => '所有') + $specialList;
//     	} 

    	if($all){
    		$specialList = array('' => '所有') + $specialList;
    	}
    	$this->addFilter('special_id', 'select', '专服', array(
    			'options'   => $specialList,
    			'value'     => empty($specialList) ? '' : key($specialList)
    	));
    
    	return $this;
    }
    
    public function addSpecial_s($all = true){
    	$user = AccAuth::me();
    	$specialList = array();
    	$specialList = SpecialServerService::specialList();

    
    	if($all){
    		$specialList = array('' => '请选择') + $specialList;
    	}
    	$this->addFilter('special_id_s', 'select', '专服', array(
    			'options'   => $specialList,
    			'value'     => empty($specialList) ? '' : key($specialList)
    	));
    
    	return $this;
    }
    
    
    
    /**
     * 获取渠道列表（下拉列表）
     * @return PubFilter
     */
    public function addOssChannel($all = false){
    	$game = PubBase::getCurrentGame();
    	$list = PubServer::getPacketOptions($game, 'id'); 
    	if($all){
    		$list = array('0' => '所有') + $list;
    	}  
    	$this->addFilter('channel_id', 'select', '渠道', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    
    	return $this;
    }

    public function addSeaOsFilter($all = false){
    	$options = PubAuthSession::get('os');        
        if($all){
            $options = array_merge(array('ALL' => 'ALL'), $options);
        }

        $this->addFilter('os', 'searchableSelect', '渠道', array(
            'options'   => $options,
            'value'     => key($options)
        ));
        return $this;
    }
    
    public function addSeaOsFilterAll(){
    	$options = array('' => '请选择');
    	$this->addFilter('os', 'searchableSelect', '渠道', array(
    			'options'   => $options,
    			'value'     => key($options)
    	));
    	return $this;
    }
    
    public function addOsFilter($lod_special_id,$all = false,$l_type=0){
    	$options = PubAuthSession::get('os');
    	//$options = array('android' => 'Android', 'ios' => 'iOS');
    	//$options = array('101'=>'101');
    	$log_s = PubRequest::R('log_s');
    	if(!isset($log_s) && $l_type == 1){
    		$params_s = PubAuthSession::get('params_s');
    		$lod_special_id = $params_s['special_id'];
    	}
    	
    	$ana_s = PubRequest::R('ana_s');
    	if(!isset($ana_s) && $l_type == 2){
    		$params_s = PubAuthSession::get('ana_s');
    		$lod_special_id = $params_s['special_id'];
    	}

    	if(!isset($lod_special_id)){  //在第一次进入系统，默认用专服列表的第一个
    		$special_array = $this->filter['special_id']['args']['options'];
    		if($special_array){	
    			$special_key_array = array_keys($special_array);
    			$lod_special_id = $special_key_array[0]; 			
    		}	
    	}
    	
    	if(!isset($lod_special_id)){ //此时该判断的情况都判断还是没有取到专服id，则说明该用户没有任何操作的渠道，即没有任何操作的专服
    		$options = array();
    	}else{
    		$modChannel = new ModChannel();
    		$channel = $modChannel->getChannelForSpecial($lod_special_id);
    		$channelList = array();
    		foreach ($channel as $key=>$val){
    			$channelList[$val['tab']] = $val['channel_name'];
    		}
    		$options = array_intersect($channelList,$options);  //取合集
    	}

    	
    	
    
    	$user = AccAuth::me();
    	if($all && $user['IsViewAll'] == 1){
    		//$options = array_merge(array('ALL' => 'ALL'), $options);
    		$options =  $options + array('ALL' => 'ALL');
    	}
    	
    	$option_key = array_keys($options);
    	PubAuthSession::set('os_one', $option_key[0]);  //这里解决存在渠道未指定服务器时，默认出现的服务器问题
    
    	$this->addFilter('os', 'searchSelect', '渠道', array(
    			'options'   => $options,
    			'value'     => key($options)
    	),$l_type);
    	return $this;
    }

    /**
     * 增加游戏节点
     * @return $this
     */
    public function addPlatformFilter($all = false) {
        $game = PubBase::getCurrentGame();
        $options = PubBase::getPlatform(true, $game);

        if ($all)
            $options = array('' => '所有平台') + $options;
        
        $p = PubRequest::R('p');
        
  		if(!empty($p)){
  			$cookie 		= new CHttpCookie('defaultP', PubRequest::R('p'));
  			$cookie->expire = time()+60*60*24*30;
  						
  			Yii::app()->request->cookies['defaultP'] = $cookie;
  		}else{
  			$cookie = Yii::app()->request->getCookies();
  			$p = $cookie['defaultP'];
  			$p = $p->value;
  		}

        $this->addFilter('p', 'select', PubFunc::t('平台'), array(
            'options' => $options,
            'value' => !empty($p) ? $p : key($options)
        ));

        return $this;
    }

    /**
     * 增加游戏节点
     * @return $this
     */
    public function addGameFilter($all = false) {
        $options = PubBase::getGame(true);

        if ($all)
            $options = array('' => '所有游戏') + $options;

        $this->addFilter('g', 'select', PubFunc::t('游戏'), array(
            'options' => $options,
            'value' => empty($options) ? '' : key($options)
        ));

        return $this;
    }
    

    public function addStateFilter($all = false) {
        $stateTab = PreviewService::getStateTab();

        if ($all)
            $stateTab = array('ALL' => PubFunc::t('所有国家')) + $stateTab;

        $this->addFilter('state', 'select', PubFunc::t('国家'), array(
            'options' => $stateTab,
            'value' => key($stateTab),
            'chosen' => true,
            'style' => 'width: 120px;'
        ));

        return $this;
    }

    public function addShow($showOptions, $value) {
        $this->addFilter('show', 'radio', PubFunc::t('展示'), array('options' => $showOptions, 'value' => $value, 'enforce' => true));
        return $this;
    }

    public function addCurrencyFilter() {
        $this->addFilter('currency', 'select', PubFunc::t('币种'), array(
            'options' => array(
                'USD' => PubFunc::t('美元'),
                'RMB' => PubFunc::t('人民币')
            ),
            'value' => 'USD'
        ));

        return $this;
    }
    
    public function addPort($l_type = 0) {
    	$options = array('3309'=>'正式服','3308'=>'测试服');//$options = array('3308'=>3308,'3309'=>3309);
    	$this->addFilter('port', 'select', PubFunc::t('端口号'), array(
    			'options' => $options,
    			'value' => empty($options) ? '' : key($options)
    	),$l_type);
    
    	return $this;
    }
    
    /**
     * 货币类型
     * @return PubFilter
     */
    public function addCoin($all = true){
    	$coinTypes = CoinService::getCoinType();
    	if ($all){
    		$coinTypes = array('' => PubFunc::t('全部')) + $coinTypes;
    	}   	
    	$this->addFilter('currency_type', 'select', PubFunc::t('货币'), array('options' => $coinTypes, 'value' => key($coinTypes)));
    	return $this;
    }
    
    /**
     * 货币、物品的获取途径
     * @param string $all
     */
    public function addAccess($field,$all = true){
    	$accessList = PubDict::dict('mango', 'waytype');
    	if ($all){
    		$accessList = array('' => '所有') + $accessList;
    	}    	
    	$this->addFilter($field, 'searchableSelect', '获取途径', array(
    			'options'   => $accessList,
    			'value'     => empty($accessList) ? '' : key($accessList)
    	));
    	return $this;
    }
    
    /**
     * 流水类型 （获得、消耗）
     * @param string $all
     * @return PubFilter
     */
    public function addStreamType($all = true){
    	$list = PubDict::dict('mango', 'streamtype');
    	if ($all){
    		$list = array('' => '所有') + $list;
    	}
    	$this->addFilter('type', 'select', '流水类型', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    	return $this;
    }
    
    /**
     * 外观类型
     * @param string $all
     * @return PubFilter
     */
    public function addAppearanceType($all = true){
    	$list = PubDict::dict('mango', 'appearancetype');
    	if ($all){
    		$list = array('' => '所有') + $list;
    	}
    	$this->addFilter('appearance_type', 'select', '外观类型', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    	return $this;
    }
    
    /**
     * 副本类型
     * @param string $all
     * @return PubFilter
     */
    public function addInstType($all = true){
    	$list = PubDict::dict('mango', 'insttype');
    	if ($all){
    		$list = array('' => '所有') + $list;
    	}
    	$this->addFilter('inst_type', 'select', '副本类型', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    	return $this;
    }
    
    /**
     * 副本地图
     * @param string $all
     * @return PubFilter
     */
    public function addInstMap($all = true){
    	$list = PubDict::dict('mango', 'mapname');
    	if ($all){
    		$list = array('' => '所有') + $list;
    	}
    	$this->addFilter('map_id', 'searchableSelect', '地图', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    	return $this;
    }
    
    /**
     * 装备部位
     * @param string $all
     * @return PubFilter
     */
    public function addSlot($all = true){
    	$list = PubDict::dict('mango', 'slottype');
    	if ($all){
    		$list = array('' => '所有') + $list;
    	}
    	$this->addFilter('slot', 'select', '装备部位', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    	return $this;
    }
    
    /**
     * 套装类型
     * @param string $all
     * @return PubFilter
     */
    public function addSuitType($all = true){
    	$list = PubDict::dict('mango', 'suittype');
    	if ($all){
    		$list = array('' => '所有') + $list;
    	}
    	$this->addFilter('suit_type', 'select', '套装类型', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    	return $this;
    }
    
    /**
     * 穿戴类型
     * @param string $all
     * @return PubFilter
     */
    public function addWeartype($all = true){
    	$list = PubDict::dict('mango', 'weartype');
    	if ($all){
    		$list = array('' => '所有') + $list;
    	}
    	$this->addFilter('type', 'select', '穿戴类型', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    	return $this;
    }
    
    /**
     * 排名榜类型
     * @param string $all
     * @return PubFilter
     */
    public function addRankType($all = true){
    	$list = PubDict::dict('mango', 'ranktype');
    	if ($all){
    		$list = array('' => '所有') + $list;
    	}
    	$this->addFilter('type', 'select', '排名榜类型', array(
    			'options'   => $list,
    			'value'     => empty($list) ? '' : key($list)
    	));
    	return $this;
    }
    
    public function addServerFilterALL($all = true,$special_id,$server_id) {
    	$options = array();
    	if($special_id != '' || $server_id != ''){
    		//查询$special_id下的服务器   		
    		$modSpecialChannel = new ModSpecialChannel();
    		$data = $modSpecialChannel->serverForSpecialId($special_id);
    		foreach ($data as $key=>$val){
    			$options[$val['server_id']] = $val['server_name'];
    		}
    	}
    	if ($all){
    		$options = array('' => PubFunc::t('所有')) + $options;
    	}
    
    	$this->addFilter('server_id', 'select', PubFunc::t('服'), array(
    			'options' => $options,
    			'value' => empty($options) ? '' : key($options)
    	));
    
    	return $this;
    }
    

    public function addServerFilter1($all = true, $split = false) {

        $platform = PubBase::getCurrentPlatform();
        $game = PubBase::getCurrentGame();

        $options = PubServer::getOptions($platform, $game);

        if ($all){
            $options = array('ALL' => PubFunc::t('全服')) + $options;
        }

        $this->addFilter('server_online', 'searchableSelect', PubFunc::t('服'), array(
            'options' => $options,
            'value' => empty($options) ? '' : key($options)
        ));

        return $this;
    }
    
    public function addServerFilter($lod_special_id,$lodos,$all = true, $split = false,$l_type = 0) {
    	$user = AccAuth::me();
    	
    	//判断是否日志类且由菜单点击进来的查询start
    	$log_filter = PubRequest::R('log_filter');
    	$log_s = PubRequest::R('log_s');
    	if(!isset($log_s) && $l_type == 1){
    		$params_s = PubAuthSession::get('params_s');
    		$lod_special_id = $params_s['special_id'];
    		$lodos = $params_s['os'];
    		
    	}
    	//判断是否日志类且由菜单点击进来的查询end
    	
    	$ana_s = PubRequest::R('ana_s');
    	if(!isset($ana_s) && $l_type == 2){
    		$params_s = PubAuthSession::get('ana_s');
    		$lod_special_id = $params_s['special_id'];
    		$lodos = $params_s['os'];
    	
    	}
    	
    	
    	if($lodos == 'ALL' && $user['IsViewAll'] == 1){ 
    		//如果为all，查询专服下的服务器
    		$game = PubBase::getCurrentGame();
    		$modServer = new ModServer();
    		$list = $modServer->getServerForSpecialId($game,$lod_special_id);
    		$options = array();
    		foreach ($list as $key=>$val){
    			$options[$val['develop_id']] = $val['server_name'].' ('.$val['server_num'].')';
    		}
    		  		
//     		$platform = PubBase::getCurrentPlatform();
//     		$game = PubBase::getCurrentGame();
//     		$options = PubServer::getOptions($platform, $game);
    		
    	}else{
    		$os_options = PubAuthSession::get('os');
    		$os_array = array();
    		foreach ($os_options as $key=>$val){
    			$os_array[] = $key;
    		}

    		$options = array();
    		if($os_array){
    			if(in_array($lodos, $os_array)){
    				$os = $lodos;
    			}else{
    				$os = PubAuthSession::get('os_one'); 
    				//$os = $os_array[0];
    			}
    			$game = PubBase::getCurrentGame();
    			$modChannel = new ModChannel();
    			$server_list = $modChannel->getServer($os,$game);
    			
    			foreach ($server_list as $key=>$val){
    				$options[$val['develop_id']] = $val['server_name'].' ('.$val['server_num'].')';
    			}
    		}
    	}
        
        if ($all){
        	$user = AccAuth::me();
        	if($user['IsViewAll'] == 1){
        		$options = array('ALL' => PubFunc::t('全服')) + $options;
        	}
        	
        }
        //$options = array();
        $this->addFilter('server', 'searchSelect', PubFunc::t('服'), array(
        		'options' => $options,
        		'value' => empty($options) ? '' : key($options)
        ),$l_type);
        
    	return $this;
    }
    
    /**
     * 平台数据获取游戏
     */
    public function addGameFilterForPl($unsetAll=false){

    	$p = $_REQUEST['p'];
    	
    	//ACC存的是r2games，需要转换一次
    	if($p == 'en'){
    		$p = 'r2games';
    	}

    	$options = PubGame::getGameForSel($p);
        if($unsetAll)  unset($options[""]);
    	$this->addFilter('g', 'select', PubFunc::t('游戏'), array('options' => $options, 'chosen' => true, 'value' => empty($options)?'':key($options)));
    	 
    	return $this;
    }
    
    public function addGameFilterFromOss($allGames=false){
    	
    	$p   = $this->get()['p'];
    	$g	 = $this->get()['gameid'];
    	$p   = empty($p)?'en':$p;
    	$isM = PubFunc::isMobile($p, 'p');
    	$url = Yii::app()->params['R2Domain']['platform'].'?ct=game&ac=data';
    	$cache = 'OSS_WEBGAME';
    	
    	if($allGames){
    		$cache = 'OSS_ALLGAME';
    		$url   = Yii::app()->params['R2Domain']['platform'].'?ct=game&ac=data&allGames=true';
    	}else if($isM){
    		$cache = 'OSS_MOBILEGAME'; 
    		$url   = Yii::app()->params['R2Domain']['platform'].'?ct=game&ac=data&mobile=1';
    	}
    	
    	$options  = Yii::app()->dataCache->get($cache);

    	if($options == false){
    		$options = array();
    		$result  = PubFunc::request($url);
    		$result  = json_decode($result['result'], true);
    		file_put_contents('/tmp/cache.log', var_export($result, true), FILE_APPEND);
    		
    		
    		foreach($result as $key => $val){
    			$options[$key] = $val['name'];
    		}
    		$options = array('ALL' => Pubfunc::t('ALL')) + $options;
    		Yii::app()->dataCache->set($cache, $options, 3600);
    	}
   		
   		$this->addFilter('gameid', 'select', PubFunc::t('游戏'), array('options' => $options, 'chosen' => true, 'value' => empty($options)?'':key($options)));
   		
   		return $this;
    }
    
    public function addServerFilterFromOss($all=true){
    	$tmp = $this->get();
    	$g   = $tmp['gameid'];
    	$cache   = 'OSS_GAME_SERVER_'.$g;
    	$options = Yii::app()->dataCache->get($cache);
    	
    	if($options == false){
    		$url 	 = 'http://www.r2games.com/?ct=game&ac=data&game_id='.$g;
    		$res 	 = PubFunc::request($url);
    		$res 	 = json_decode($res['result'], true);
    		$options = array();
    		 
    		foreach($res as $key => $val){
    			$options[$key] = $val['servername'];
    		}
    		
    		if($all)
    			$options = array('ALL' => 'ALL') + $options;
    		
    		if(empty($options))
    			$options = array('--' => '--');
    		
    		Yii::app()->dataCache->set($cache, $options, 3600);	
    	}
    	 
    	$this->addFilter('server', 'select', PubFunc::t('服'), array('options' => $options, 'chosen' => true, 'value' => empty($options)?'ALL':key($options)));
    	 
    	return $this;
    }
    
    
    public function addServerFilterForPl($all=true){
    	$g = $_REQUEST['g'];
    	$p = $_REQUEST['p'];
    	
    	if($p == 'r2games'){
    		$p = 'en';
    	}

    	$PubEtl  = new PubEtl();
    	$options = $PubEtl->getServerForPl($p, $g);

    	if($all)
    		$options = array('ALL' => Pubfunc::t('ALL')) + $options;

    	if(empty($options))
    		$options = array('--' => '--');

    	$this->addFilter('server', 'select', PubFunc::t('服'), array('options' => $options, 'chosen' => true, 'value' => empty($options)?'ALL':key($options)));

    	return $this;
    	
    }

    public function addPeriod() {

        $periodOptions = array(
            'Daily' => PubFunc::t('日'),
            'Weekly' => PubFunc::t('周'),
            'Monthly' => PubFunc::t('月')
        );

        $this->addFilter('period', 'select', PubFunc::t('周期'), array('options' => $periodOptions, 'value' => 'Daily'));

        return $this;
    }

    public function addTime($default = 7) {

        if (is_numeric($default)) {
            $value = PubDateRange::getLastDayRange($default);
        } else {
            switch ($default) {
                case 'yesterday':
                    $value = PubDateRange::getYesterdayRange();
                    break;
                case 'today' :
                    $value = PubDateRange::getTodayRange();
                    break;
                default :
                    $value = $default . '~' . $default;
            }
        }

        $this->addFilter('time', 'timerange', PubFunc::t('时间'), array('value' => $value));
        return $this;
    }
    
    public function addTimeInput($date='', $name='date', $label='', $type='day'){
    	
    	if(empty($date)){
    		$date = 'today';
    	}
    	 
    	switch ($date){
    		case 'today' :
    			$date = date('Y-m-d');
    			break;
    		case 'yesterday':
    			$date = date('Y-m-d', strtotime('-1 day'));
    			break;
    		case 'none':
    			$date = '';
    			break;
    		case 'firstday':
    			$date = date('Y-m-01');
    			break;
    		default :
    			$date = date('Y-m-d', strtotime($date.' days'));
    	}
    	 
    	if(empty($label)){
    		$label = PubFunc::t('日期：');
    	}else if($label == 'none'){
    		$label = '';
    	}

        $this->addFilter($name, 'input', $label, array('date' => 'day', 'value' => $date));

    	return $this;
    }

    // 增加渠道筛选项
    public function addAffiliateChannel() {

        $options = AffiliateService::getUser();

        if (isset($_COOKIE['ck_af_channel']) && isset($options[$_COOKIE['ck_af_channel']])) {
            $value = $_COOKIE['ck_af_channel'];
        } else {
            $value = key($options);
        }

        $the = PubRequest::R('af_channel');
        if (!empty($the)) {
            setcookie('ck_af_channel', $the, time() + 86400);
        }

        $this->addFilter('af_channel', 'select', 'User', array(
            'options' => $options,
            'value' => $value,
            'chosen' => true
        ));

        return $this;
    }

    public function addAffiliateGame() {
        $gameOptions = AffiliateService::getGameOptions();

        $this->addFilter('af_game', 'select', 'Game', array(
            'options' => $gameOptions,
            'value' => key($gameOptions)
        ));

        return $this;
    }

    public function addUnionManager(){
        $game = PubRequest::R('union_game');
        $options = array('' => '---') + UnionService::getUnion($game);

        $this->addFilter('union_id', 'select', '联运商', array(
            'options'   => $options,
            'value'     => key($options),
            'chosen'    => true
        ));

        return $this;
    }

    public function addUnionGame(){
        $unionId = PubRequest::R('union_id');
        $options = array('' => '---') + UnionService::getGame($unionId);

        $this->addFilter('union_game', 'select', '游戏', array(
            'options'   => $options,
            'value'     => key($options),
            'chosen'    => true
        ));

        return $this;
    }

    public function addGameForFinPl(){
        $options = FinanceService::getFinGames();
        $this->addFilter('game', 'select', '游戏', array('options' => $options, 'chosen' => true, 'style' => 'width: 180px;'));
        return $this;
    }
    public function addFinPl(){
        $ops = array_merge(array('en'=>'英语'), PreviewService::getPlatTab());
        unset($ops['r2games']);
        $this->addFilter('platform', 'select', '平台', array('options' => $ops, 'chosen' => true, 'style' => 'width: 180px;'));
        return $this;
    }
    public function addSaCountry($unsetAll=false){
        $options = PubFunc::dict('CDNCountry');
        if($unsetAll){
            unset($options['ALL']);
            $value = key($options);
        }else
            $value = 'ALL';
        $this->addFilter('country', 'select', PubFunc::t('Country'), array('options' => $options, 'chosen' => true, 'value' => $value));
        return $this;
    }
    public function addSaRegion($unsetCountryAll=false, $unsetAll=false){
        $country = $_REQUEST['country'];
        if(empty($country)){
            $countryList = PubFunc::dict('CDNCountry');
            if($unsetCountryAll){
                unset($countryList['ALL']);
                $country = key($countryList);
            }
            else
                $country = 'ALL';
        }
        $options = PubFunc::dict("CDNRegion_{$country}");
        if($unsetAll){
            unset($options['ALL']);
            $value = key($options);
        }else
            $value = 'ALL';
        $this->addFilter('region', 'select', PubFunc::t('Region'), array('options' => $options, 'chosen' => true, 'value' => $value));
        return $this;
    }

    public function addCopublisherForPl(){
        $options = AffiliateService::getCopublisher();
        $this->addFilter('copublisher', 'select', PubFunc::t('Copublisher'), array('options' => $options, 'chosen' => true, 'value' => empty($options)?'':key($options)));
        return $this;
    }
    public function addTagFilterForPl(){
        $p = (PubRequest::R('p')=='r2games')?'en':PubRequest::R('p');
        $copublisher = PubRequest::R('copublisher');
        $options = AffiliateService::getTag($p, $copublisher);
        $this->addFilter('tag', 'select', PubFunc::t('Tag'), array('options' => $options, 'chosen' => true, 'value' => empty($options)?'':key($options)));
        return $this;
    }
    public function addPlatformFilter2($all=false){
        $options = array('r2games'=>'英语', 'fr'=>'法语', 'de'=>'德语', 'br'=>'葡萄牙语', 'tr'=>'土耳其语', 'ru'=>'俄语');
        if ($all)
            $options = array('' => '所有平台') + $options;
        $this->addFilter('pl', 'select', PubFunc::t('平台'), array(
            'options' => $options,
            'value' => !empty($p) ? $p : key($options)
        ));
        return $this;
    }
    
    public function userTypes(){
    	$options = array('1'=>'所有用户','2'=>'非开服第一天注册用户');
    	$this->addFilter('user_type', 'radio', PubFunc::t('用户'), array('options' => $options, 'value' => key($options)));
    	return $this;

    }
    
    /**
     * 根据用户具备的渠道得到专服
     * @param string $all
     * @return PubFilter
     */
    public function addSpecialFilter($all=false,$l_type=0){
    	//查询当前用户的有权限的渠道
    	$options = PubAuthSession::get('os');
    	$specialList = array();
    	if($options){
    		$tabs = '';
    		foreach ($options as $key=>$val){
    			$tabs .= ','.'"'.$key.'"';
    		}
    		$tabs = substr($tabs, 1);
    	    
    		$modChannel = new ModChannel();
    		$special = $modChannel->getSpecialForChannel($tabs);
    		$specialList = array();
    		foreach ($special as $key=>$val){
    			$specialList[$val['special_id']] = $val['name'];
    		}
    	}
    	
    	if($all){
    		$specialList =  array('ALL' => 'ALL') + $specialList ;
    	}
    	
    	$this->addFilter('special_id', 'searchSelect', '专服', array(
    			'options'   => $specialList,
    			'value'     => key($specialList)
    	),$l_type);
    	return $this;
    	
    }
    
    /**
     * 根据用户具备的渠道得到专服
     * @param string $all
     * @return PubFilter
     */
    public function addSeaSpecialFilter($all=false){
    	//查询当前用户的有权限的渠道
    	$options = PubAuthSession::get('os');
    	$specialList = array();
    	if($options){
    		$tabs = '';
    		foreach ($options as $key=>$val){
    			$tabs .= ','.'"'.$key.'"';
    		}
    		$tabs = substr($tabs, 1);
    			
    		$modChannel = new ModChannel();
    		$special = $modChannel->getSpecialForChannel($tabs);
    		$specialList = array();
    		foreach ($special as $key=>$val){
    			$specialList[$val['special_id']] = $val['name'];
    		}
    	}
    	 
    	if($all){
    		$specialList =  array('' => '请选择') + $specialList ;
    	}
    	 
    	$this->addFilter('special_id', 'searchableSelect', '专服', array(
    			'options'   => $specialList,
    			'value'     => key($specialList)
    	));
    	return $this;
    	 
    }
    
}
